package com.itheima.Demo;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.List;

public class DemoXpath {
    public static void main(String[] args) throws Exception {
        //创建解析对象
        SAXReader reader = new SAXReader();
        //读取文件
        Document read = reader.read("Day22codes\\tianqi.xml");
        //绝对路径--------------------------------------------------------------------------
        //List selectNodes("表达式")				：使用Xpath作为参数查找多个节点
        //  以单/开头叫绝对路径,绝对路径必须从根元素开始写
        List<Element> list = read.selectNodes("/天气预报/上海");
        for (Element element : list) {
            System.out.println(element);
        }
        System.out.println("=================================");
        //Node selectSingleNode("表达式")     ：使用Xpath作为参数查找某个节点（默认获
        Node node = read.selectSingleNode("/天气预报/上海");
        System.out.println(node.getName());
        System.out.println("=================================");
        //相对路径------
        //不以单/开头的路径叫相对路径,相对于当前元素
        Node node1 = read.selectSingleNode("/天气预报/上海/温度");
        System.out.println(node1);
        Node node2 = node1.selectSingleNode("../湿度");
        System.out.println(node2.getName()+node2.getText());
        System.out.println("=================================");
        //全文搜索
        //获取文本中的所有最高温度
        List<Element> list1 = read.selectNodes("//湿度");
        for (Element element : list1) {
            System.out.println(element.getName()+element.getText());
        }
    }
}
